<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

require_once 'dbconnector.php';

function drawpoll($pollid,$userid)
{
    $output="";
    $query="SELECT * FROM pollingfield WHERE id=$pollid;";
    $result=mysql_query($query);

    $rows=mysql_fetch_array($result);
    $title=$rows['title_polling'];

    $query="SELECT * FROM pollingoption WHERE pollingfield_id=$pollid;";
    $result=mysql_query($query);
    $i=0;
    $total=0;
    $opt_count=mysql_num_rows($result);
    while($rows=mysql_fetch_array($result))
    {
        $option[$i++]=$rows;
        $total+=$rows['jumlah'];
    }
    $query="SELECT * FROM hasvote WHERE pollingfield_id=$pollid AND userid=$userid";
    $result=mysql_query($query);
    $hasvote=  mysql_num_rows($result)>0;
    $output=
    "<div class='poll' id='poll$pollid'>
        <p>$title</p>
        <table class='poll-graph' id='graph$pollid' width='655px' border='0' cellspacing='10'>
            <tbody>            
            <tr>";
    $background="#317798";
    foreach($option as $rows)
    {
        $optid=$rows['id'];
        $name=$rows['name'];
        $heightpx=0;
        if($total!=0)
        {
            $heightpx=$rows['jumlah']*100/$total;
        }
        $output.=
        "<td align='center' valign='bottom' id='graph$pollid-$optid'>
            <div class='poll-diagram' style='width: 25px; height: $heightpx"."px; background-color: $background' title='$name'></div>
        </td>";
    }
    $output.="</tr>
        <tr valign='bottom' align='center' id='vote$pollid'>";
    foreach($option as $rows) {
        $name=$rows['name'];
        $jumlah=$rows['jumlah'];
        $percent=0;
        if($total!=0)
        {
            $percent=$jumlah*100/$total;
            $percent=number_format($percent,2);
        }
        $output.=
        "<td valign='botom' align='center'>
            <span>
                <b>
                <p>
                $name
                </p>
                <br>
                <p>
                $percent%
                </p>
                <br>
                </b>
                <b></b>
            </span>
            </td>";
    }
    $output.="</tr>";
    if(!$hasvote)
    {
        $output.="<tr valign='bottom' align='center'>";
        foreach($option as $rows)
        {
            $optid=$rows['id'];
            $output.="<td valign='bottom' align='center'>
                <form name='vote_form' action='Process/update.php' method='post'>
                    <input name='pollingfield_id' value='$pollid' style='display:none' />
                    <input name='pollingoption_id' value='$optid' style='display:none' />
                    <input type='submit' value='Pilih'/>
                </form>
                </td>";
        }
        $output.="</tr>";
    }
    $output.=
        "</tbody></table>
    </div>";
    return $output;
}
?>
